//
// See the file COPYING for redistribution information.
//

#include <iostream>
#include <cstring>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <Naipax.hpp>

using namespace std;
using namespace Naipax;

void ResponseHandler(Connection &rConn,
                     string &rUrl, string &rResponse) {
    cout << endl
         << "[Page Count] " << rConn.CompletedCount() << endl
         << "[Url] " << rUrl << endl
         << "[Length] " << rResponse.length() << endl << endl;
}

int main() {
    cerr << "Test for the Connection module" << endl;

    try {
        static const uint32_t sUrlListSize = 500;
        char UrlList[sUrlListSize][URL_SIZE] = {
            "http://mrbacklink.com/",
            "http://shopdd.blog51.fc2.com/",
            "http://www.cell-gadgets.com/",
            "http://europe.nokia.com/A4305010",
            "http://www.internettablettalk.com",
            "http://www.orb.com/n800",
            "http://maemo.org/asdf.cgi?key=value",
            "http://www.gizmovoip.com/site,eseries/learn/",
            "http://www.scottberkun.com/essays/53-how-to-detect-bullshit/",
            "http://www.michaelshannon.us/makeabook/index.html",
            "http://www.danah.org/SNSResearch.html",
            "http://www.kk.org/cooltools/archives/001782.php",
            "http://www.fieldlines.com/story/2007/1/28/215243/977",
            "http://www.findarticles.com/",
            "http://www.stardust.tv/",
            "http://www.keithball.net/dod/",
            "http://www.marumushi.com/apps/newsmap/newsmap.cfm",
            "http://www.mainada.net/inputdraw",
            "http://blog.wired.com/gadgets/2007/07/ten-reasons-to-.html",
            "http://strangepaths.com/interstellar-ark/2007/02/14/en/",
            "http://www.fontshop.com/fontfeed/archives/web-20-logos.cfm",
            "http://www.pixellogo.com/",
            "http://brandsoftheworld.com/",
            "http://www.sidestep.com/air/",
            "http://www.orbitz.com/",
            "http://www.redhotpawn.com/",
            "http://www.chesscafe.com/",
            "http://www.chessgames.com/",
            "http://www.billshare.org/",
            "http://www.nextthing.org/archives/2005/08/07/fun-with-http-headers",
            "http://code.nanigac.com/",
            "http://www.scottberkun.com/essays/53-how-to-detect-bullshit/",
            "http://www.michaelshannon.us/makeabook/index.html",
            "http://www.vaed.uscourts.gov/notablecases/moussaoui/exhibits/",
            "http://adrianspeyer.blogspot.com/",
            "http://www.helium.com/tm/502261/leaders-achieve-leadership-leadership",
            "http://www.nytimes.com/",
            "http://www.alternet.org/story/55508/",
            "http://news.com.com/",
            "http://techdirt.com/articles/20070801/011957.shtml",
            "http://business.nikkeibp.co.jp/article/tech/20070801/131447/",
            "http://www.prop1.org/prop1/azantink.htm",
            "http://guitar.about.com/library/blguitarlessonarchive.htm",
            "http://www.guitarplayertv.com/",
            "http://cityrag.blogs.com/main/2007/01/100_greatest_gu.html",
            "http://www.happiness-project.com/happiness_project/",
            "http://www.fortysomething.ca/mt/etc/archives/006129.php",
            "http://www.engadget.com/",
            "http://www.zoho.com/",
            "http://vixy.net/",
            "http://www.daviscup.com/",
            "http://www.youtube.com/watch?v=mhGi0F3ms5w",
            "http://www.degreetutor.com/library/career-starter/115-secrets",
            "http://tantek.com/log/2007/02.html#d19t1813",
            "http://techjive.net/2007/02/02/excel-keystrokes/",
            "http://www.pcmag.com/article2/0,1895,2164136,00.asp",
            "http://www.37signals.com/svn/posts/542-the-5-10-20-year-plan",
            "http://www.hotswap.com/",
            "http://finance.yahoo.com/expert/article/careerist/40342",
            "http://www.newser.com/",
            "http://www.catonmat.net/blog/sed-stream-editor-cheat-sheet/",
            "http://cre.ations.net/blog/post/tether-your-iphone",
            "http://blackfire.tw-blog.net/blog/blog_notes.php?username=blackfire",
            "http://www.bgo.com.tw/",
            "http://photo.xuite.net/franklin0727*3",
            "http://www.wretch.cc/album/yaya840322",
            "http://www.taipeitimes.com/News/taiwan/archives/2007/08/01/2003372218",
            "http://tw.news.yahoo.com/article/url/d/a/070802/2/i6v4.html",
            "http://photo.pchome.com.tw/chilinglin/",
            "http://www.wretch.cc/album/chingwen0906",
            "http://www.taipeitimes.com/News/front/archives/2007/08/02/2003372356",
            "http://errtheblog.com/post/7708",
            "http://www.theatlantic.com/slideshows/made-in-china/",
            "http://www.officesnapshots.com/",
            "http://www.alistapart.com/articles/betterwritingthroughdesign",
            "http://www.timesonline.co.uk/tol/news/world/iraq/article712424.ece",
            "http://infopirate.org/social-bookmark-search",
            "http://www.radarsync.com/vista/",
            "http://www.isnichwahr.de/redirect16975.html",
            "http://rippin-kitten.blogspot.com/2007/02/washing-cat.html",
            "http://www.lucid-tv.com/",
            "http://www.harpers.org/Cartoon.html",
            "http://www.comeeko.com/",
            "http://www.thespacereview.com/article/810/1",
            "http://news.com.com/2100-11395_3-6160372.html",
            "\0"
        };

        Connection Conn;
        Conn.mpResponseHandler = ResponseHandler;
        for (uint32_t i = 0;
             i < sUrlListSize && UrlList[i][0] != '\0'; ++i) {
            cout << "Adding connection to " << UrlList[i] << " ... ";
            try {
                Conn.Add(UrlList[i]);
                cout << "OK" << endl;
            }
            catch (Naipax::Error &E) {
                cerr << E.What() << endl;
                cout << "NOT OK" << endl;
            }
            if (Conn.CurConnCount() > 0) {
                Conn.OneNbLoop();
            }
        }

        while (Conn.CurConnCount() > 0) {
            Conn.OneNbLoop();
        }
    }
    catch (Naipax::Error &E) {
        NAIPAX_PERROR(E.What());
        return -1;
    }
    return 0;
}
